Manchester | 25-SDC-Nov | Rahwa Haile | Sprint 2 | improve_with_precomputing#128
Manchester | 25-SDC-Nov | Rahwa Haile | Sprint 2 | improve_with_precomputing#128RahwaZeslusHaile wants to merge 3 commits intoCodeYourFuture:mainfrom
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
cjyuan
left a comment
There was a problem hiding this comment.
Code looks good.
Can you use complexity to explain how the new implementation are better than the original implementation?
|
Thanks @cjyuan for that feedback! I've documented the complexity analysis in the README. |
| - Time Complexity: **O(n² × m)** - Compares every string with every other string (n²) pairs, each taking O(m) comparisons | ||
|
|
||
| **Optimized Approach (Sort + Adjacent Pairs):** | ||
| - Time Complexity: **O(n log n + n × m)** - Sort once (n log n), then compare only adjacent pairs (n comparisons) |
There was a problem hiding this comment.
If we are factoring in the length of the strings, m, then the complexity of sorting won't just be O(nlogn). nlogn measures only the number of comparisons needed.
|
|
||
| **Optimized Approach (Sort + Adjacent Pairs):** | ||
| - Time Complexity: **O(n log n + n × m)** - Sort once (n log n), then compare only adjacent pairs (n comparisons) | ||
| - **Why better:** The longest common prefix will be found in the first and last strings after sorting, eliminating unnecessary comparisons. Reduces from O(n²) to O(n) pair comparisons. |
There was a problem hiding this comment.
What do you mean by first and last strings? That sounds like only two comparisons are needed to find the longest prefix after sorting.
There was a problem hiding this comment.
Thanks @cjyuan for the detailed feedback! You're absolutely right on both points. I've updated the README to correct these issues
|
Good analysis. Well done! |
Manchester | 25-SDC-Nov | Rahwa Haile | Sprint 2 | improve_with_precomputing
Learners, PR Template
Self checklist
Description
Improves the performance of two algorithms using precomputing strategies without modifying existing tests.
Changelist
Common Prefix:
Pre-sort strings once, then compare only adjacent pairs
Reduces comparisons from O(n²) to O(n log n) sort + O(n) comparisons
Count Letters:
Precompute lowercase letters into a set during the first pass
Replace repeated letter.lower() in s checks with O(1) set lookups
Reduces time complexity from O(n²) to O(n)
I really appreciate the time you take to review this PR